package com.zmm.three.party.config;

import jodd.util.StringUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * author 900045
 * description:
 * name MyRedisSonConfig
 * dates By 2021-06-28 16:39:55
 */
@Configuration
public class MyRedisSonConfig {

	@Value("${spring.redis.host}")
	private String redisIp;


	@Value("${spring.redis.port}")
	private String redisPort;

	@Value("${spring.redis.password}")
	private String redisPassword;
	
	
	@Value("${spring.redis.database}")
	private Integer database;


	/**
	 * 所有对  RedisSon 的使用 都是通过 RedisSonClient 对象
	 * return
	 * throws IOException
	 */

	@Bean(destroyMethod = "shutdown")
	public RedissonClient redissonClient() {
		Config config = new Config();
		String address = "redis://"+redisIp+":"+redisPort;
		SingleServerConfig singleServerConfig = config.useSingleServer();
		singleServerConfig.setAddress(address);
		singleServerConfig.setDatabase(database);
		if (StringUtil.isNotBlank(redisPassword)){
			singleServerConfig.setPassword(redisPassword);
		}
		config.setCodec(JsonJacksonCodec.INSTANCE);
		// 本例子使用的是yaml格式的配置文件，读取使用Config.fromYAML，如果是Json文件，则使用Config.fromJSON
		// 项目启动 并不能读取 redis-config.yml 文件
		//Config config = Config.fromYAML(MyRedisSonSetting.class.getClassLoader().getResource("redis-config.yml"))
		return Redisson.create(config);
	}
}
